package com.jxmcloud.business.cut.service.impl.database;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxmcloud.business.cut.mapper.database.IUserLoginInfoDBMapper;
import com.jxmcloud.business.cut.model.database.DBUserLoginInfo;
import com.jxmcloud.business.cut.service.database.IUserLoginInfoDBService;
import com.jxmcloud.business.cut.vo.toDb.ToDbUserBase;
import com.jxmcloud.business.cut.vo.user.RankBuyOrderVo;
import com.xlcloud.platform.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class UserLoginInfoDBServiceImpl extends ServiceImpl<IUserLoginInfoDBMapper, DBUserLoginInfo> implements IUserLoginInfoDBService {
    @Override
    public boolean insertIntoData(DBUserLoginInfo info) {
        return this.baseMapper.insert(info) == 1;
    }

    @Override
    public DBUserLoginInfo selectOne(Integer uid) {
        return this.baseMapper.selectById(uid);
    }

    @Override
    public List<DBUserLoginInfo> selectAll(List<Integer> uid) {
        QueryWrapper<DBUserLoginInfo> wrapper = new QueryWrapper<>();
        wrapper.in("uid", uid);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public DBUserLoginInfo getUserList(String account) {
        if (StringUtils.isEmpty(account)) {
            return null;
        }
        QueryWrapper<DBUserLoginInfo> wrapper = new QueryWrapper<>();
            wrapper.in("account", account);
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public boolean deleteData(Integer uid) {
        return this.baseMapper.deleteById(uid) == 1;
    }

    @Override
    public boolean updateData(DBUserLoginInfo info) {
        return this.baseMapper.updateById(info) == 1;
    }

    @Override
    public DBUserLoginInfo selectByAccount(String account) {
        QueryWrapper<DBUserLoginInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("account", account);  // 添加账号查询条件
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public boolean updateUserBaseInfo(ToDbUserBase userBaseVo) {
        DBUserLoginInfo dbUserLoginInfo = this.baseMapper.selectById(userBaseVo.getUid());
        if (ObjectUtil.isNotNull(dbUserLoginInfo)) {
            // 更新基本信息
            dbUserLoginInfo.setNickName(userBaseVo.getNickName());
            dbUserLoginInfo.setXlId(userBaseVo.getXlId());
            dbUserLoginInfo.setIcon(userBaseVo.getIcon());
            dbUserLoginInfo.setShopId(userBaseVo.getShopId());
            dbUserLoginInfo.setShopName(userBaseVo.getShopName());
            dbUserLoginInfo.setSex(userBaseVo.getSex());
            dbUserLoginInfo.setPhone(userBaseVo.getPhone());
            dbUserLoginInfo.setRegisterTime(new Date(userBaseVo.getCreateTime() * 1000L));
            dbUserLoginInfo.setSessionKey(userBaseVo.getSession());
            dbUserLoginInfo.setTotalBuyNum(userBaseVo.getTotalBuyNum());
            return this.updateById(dbUserLoginInfo);
        }
        return false;
    }

    @Override
    public boolean updateUserStatusInfo(ToDbUserBase userBaseVo) {
        DBUserLoginInfo dbUserLoginInfo = selectByAccount(userBaseVo.getAccount());
        if (ObjectUtil.isNotNull(dbUserLoginInfo)) {
            dbUserLoginInfo.setStatus(userBaseVo.getStatus());
            dbUserLoginInfo.setBannedTime(null);
            if(ObjectUtil.isNotEmpty(userBaseVo.getBannedTime())){
                dbUserLoginInfo.setBannedTime(DateUtil.parseDate(userBaseVo.getBannedTime()));
            }
            return this.updateById(dbUserLoginInfo);
        }
        return false;
    }
    @Override
    public List<RankBuyOrderVo> selectRankBuyOrder() {
        return this.baseMapper.selectRankBuyOrder();
    }
}
